//
//  ViewController.m
//  book
//
//  Created by 云天 李 on 11-11-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "BookDB.h"
#import "BookIndex.h"
#import "BookContentViewController.h"

@implementation ViewController

@synthesize bookIndexes;
@synthesize bookIndexTable;
@synthesize bookContentViewController;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - UITableViewDataSource Methods

// Table的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.bookIndexes count];
}

// Table中每行TableCell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
	static NSString *CellIndentifier = @"BookIndexView";
    UITableViewCell *cell = [bookIndexTable dequeueReusableCellWithIdentifier:CellIndentifier];
    
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
		//  自动换行
		[cell.textLabel setNumberOfLines:0]; 
    }

    BookIndex *index = [self.bookIndexes objectAtIndex:indexPath.row];
    
    [[cell textLabel] setText:[index title]];

    return cell;
}


// 行高
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 70;
}

#pragma mark - UITableViewDelegate Methods

// 点击目录显示正文
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.bookContentViewController == nil)
    {
        self.bookContentViewController = [[[BookContentViewController alloc] initWithNibName:@"BookContentViewController" bundle:nil] autorelease];
    }
    
    BookIndex *index = [self.bookIndexes objectAtIndex:indexPath.row];
	NSLog(@"%d", [index indexid]);
    
    [self.bookContentViewController setIndexid:[index indexid]];
    
    [self presentModalViewController:self.bookContentViewController animated:YES];
}
    
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    if(self.bookIndexes == nil)
    {
        self.bookIndexes = [NSMutableArray array];
    }
    else
    {
        [self.bookIndexes removeAllObjects];
    }
    
    self.bookIndexes = [BookDB fetchBookIndexes];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

// 释放内存
- (void) dealloc
{
    [bookIndexTable release];
    [bookIndexes release];
    [bookContentViewController release];
    [super dealloc];
}

@end
